package com.qilu.careprogram.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;

import co.elastic.clients.elasticsearch.core.*;
import com.qilu.careprogram.pojo.entity.es.QuestionAnswer;
import com.qilu.careprogram.service.ESQuestionAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ESQuestionAnswerServiceImpl implements ESQuestionAnswerService {

    private final static String indexName = "question_answer_new";

    @Autowired
    private ElasticsearchClient esClient;

    @Override
    public String addDocument(QuestionAnswer questionAnswer) {
        try {
            IndexResponse response = esClient.index(i -> i
                    .index(indexName)
                    .document(questionAnswer));
            return response.id();
        } catch (IOException e) {
            System.err.println("异常信息：" + e);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public String addBulkDocument(List<QuestionAnswer> questionAnswerList) {

        BulkRequest.Builder builder = new BulkRequest.Builder();
        for (QuestionAnswer questionAnswer : questionAnswerList) {
            builder.operations(op -> op
                    .create(c -> c
                                    .index(indexName)
                                    .document(questionAnswer)
                    )
            );
        }
        try {
            BulkResponse response = esClient.bulk(builder.build());
            return "批量插入成功";
        } catch (IOException e) {
            System.err.println("异常信息：" + e);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<QuestionAnswer> getAllDocument(Integer page, Integer pageSize) {
        try {
            SearchResponse<QuestionAnswer> searchResponse = esClient.search(s -> s
                    .index(indexName)
                    .query(q -> q.matchAll(m -> m))
                            .from((page - 1) * pageSize)
                            .size(pageSize)
                    ,QuestionAnswer.class
            );

            List<QuestionAnswer> questionAnswerList = searchResponse.hits().hits().stream()
                    .map(hit -> {
                        QuestionAnswer questionAnswer = hit.source();
                        questionAnswer.setId(hit.id());
                        return questionAnswer;
                    })
                    .collect(Collectors.toList());
            return questionAnswerList;
        } catch (IOException e) {
            System.err.println("异常信息：" + e);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<QuestionAnswer> getByConditionDocument(String condition) {

        try {
            SearchResponse<QuestionAnswer> searchResponse = esClient.search(s -> s
                            .index(indexName)
                            .from(0)
                            .size(10)
                            .query(q -> q
                                    .multiMatch(t -> t
                                            .fields("question","content")
                                            .query(condition)))
                    , QuestionAnswer.class);

            List<QuestionAnswer> questionAnswerList = searchResponse.hits().hits().stream()
                    .map(hit -> {
                        QuestionAnswer questionAnswer = hit.source();
                        return questionAnswer;
                    })
                    .collect(Collectors.toList());

            return questionAnswerList;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String deleteDocument(String id) {
        try {
            DeleteResponse deleteResponse = esClient.delete(DeleteRequest.of(d -> d
                    .index(indexName)
                    .id(id)));
            return deleteResponse.result().jsonValue();

        }catch (IOException e){
            throw new RuntimeException("es删除失败:" + e.getMessage(),e);
        }
    }


}
